﻿@model IUserAccountViewModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>register</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <div>
            @Html.ValidationSummary()
            <fieldset>
                <legend>Registration Form</legend>
                <ol>
                    <li>
                        @Html.LabelFor(m => m.Item.Name)
                        @Html.TextBoxFor(m => m.Item.Name, new { id = "Name" })
                        @Html.ValidationMessageFor(m => m.Item.Name)
                    </li>
                    <li>
                        @Html.LabelFor(m => m.Item.Email)
                        @Html.TextBoxFor(m => m.Item.Email, new { id = "Email" })
                    </li>
                    <li>
                        @Html.LabelFor(m => m.Item.Password)
                        @Html.PasswordFor(m => m.Item.Password, new { id = "Password" })
                    </li>
                    <li>
                        @Html.LabelFor(m => m.RegisterRoleID)
                        @Html.DropDownListFor(m => m.RegisterRoleID, (IEnumerable<SelectListItem>)ViewBag.Roles, new { id = "RegisterRoleID" })
                    </li>
                </ol>
                <input type="submit" value="Register" id="submit" />
            </fieldset>
        </div>
    }
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $(function () {
            $("#submit").click(function () {
                var userAccount = getUserAccount();
                if (userAccount == null) {
                    return;
                }
                var json = $.toJSON(userAccount);
                $.ajax({
                    url: '/UserAccount/register',
                    type: 'POST',
                    dataType: 'json',
                    data: json,
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        if (data) {
                            alert(data.Messages[0]);
                        }
                    },
                    error: function () {
                    }
                })

            })
            //            $("form[action$='register']").submit(function () {
            //                $.post($(this).attr("action"), $(this).serialize(), function (response) {
            //                    alert(response.Messages[0]);
            //                });

            //                return false;
            //            });
        })

        function getUserAccount() {
            var name = $("#Name").val();
            var email = $("#Email").val();
            var password = $("#Password").val();
            var registerRoleID = $("#RegisterRoleID").val();
            return (name == "") ? null : { Item: { Name: name, Email: email, Password: password }, RegisterRoleID: registerRoleID };
        }
    </script>
</body>
</html>
